package cate.game.role.farm.udgpalace;

import cate.common.table.d.GDUdgPalace;
import cate.common.table.farm.udgpalace.row.UdgPalaceItemRow;
import cate.common.table.farm.udgpalace.row.UdgPalaceShopRow;
import cate.common.util.GameResult;
import cate.game.play.param.HpExtend;
import cate.game.play.part.FightTeam;
import cate.game.res.MixRes;
import cate.game.res.MixResAdder;
import cate.game.res.MixResConsumer;
import cate.game.role.Role;
import cate.game.role.RoleDependent;
import cate.game.role.bag.hero.Hero;
import cate.game.role.mall.Commodity;
import easy.java.dev.note.NoteClass;
import easy.java.dev.note.NoteField;
import lombok.Getter;
import org.apache.commons.lang3.StringUtils;

import java.util.*;

@NoteClass("我的地宫探险队伍数据")
public class UdgPalaceTeam extends RoleDependent {
    @NoteField("上阵英雄")
    public List<Hero> heros;

    @NoteField("血量继承")
    @Getter
    public HpExtend hpExtend;

    @NoteField("王者祝福记录 <食灵uid>")
    public List<String> wishBuff;

    @NoteField("道具 <道具栏ID，道具>")
    public Map<Integer, UdgPalaceItem> items;

    @NoteField("商店")
    public List<Commodity> mall;

    @NoteField("本期掉落奖励")
    public MixRes recordDropReward;

    @Override
    public void initialize(Role role) {
        super.initialize(role);
        if (heros == null) {
            heros = new ArrayList<>();
        }
        if (hpExtend == null) {
            hpExtend = new HpExtend();
        }
        if (wishBuff == null) {
            wishBuff = new ArrayList<>();
        }
        if (items == null) {
            items = new HashMap<>();
        }
        if (mall == null) {
            mall = new ArrayList<>();
        }
        if (recordDropReward == null) {
            recordDropReward = new MixRes();
        }
    }

    public void reset() {
        heros.clear();
        hpExtend.clear();
        mall.clear();
        wishBuff.clear();
        recordDropReward.clear();
        initItems();
    }

    public GameResult<Void> setTeam(List<Hero> heros) {
        GameResult<Void> r = new GameResult<>();
        if (this.heros != null && !this.heros.isEmpty()) {
            return r.fail("不可更改阵容");
        }
        this.heros = heros;
        return r;
    }

    public Hero getHero(String heroUid) {
        return heros.stream().filter(e -> StringUtils.equals(heroUid, e.uid)).findAny().orElse(null);
    }

    public boolean containsWishBuff(String heroUid) {
        return wishBuff.contains(heroUid);
    }

    public void afterChallenge(FightTeam ft) {
        hpExtend.recordByUid(ft);
    }

    private void initItems() {
        items.clear();
        for (UdgPalaceItemRow itemRow : role.getGame().table.udgPalace.item.list) {
            if (GDUdgPalace.ItemType.transItemType(itemRow.id) == GDUdgPalace.ItemType.SHOP) {
                addMallItems(1);
            } else {
                items.put(itemRow.id, new UdgPalaceItem(itemRow.initTimes));
            }
        }
    }

    public void updateItems(GDUdgPalace.EventType eventType, int group) {
        int pos = 0;
        switch (eventType) {
            case RECOVER:
                pos = 1;
                break;
            case KILL:
                pos = 2;
                break;
            case WISH:
                pos = 3;
                break;
            case DAMAGE:
                pos = 4;
                break;
            default:
                break;
        }
        Optional.of(items.get(pos)).ifPresent(e -> e.update(group));
    }

    public GameResult<Void> useItem(int itemId, String heroUid, int bossId) {
        GameResult<Void> r = new GameResult<>();
        UdgPalaceItemRow itemRow = role.getGame().table.udgPalace.item.get(itemId);
        if (itemRow == null) {
            return r.fail("不存在的道具id");
        }
        UdgPalaceItem item = items.get(itemId);
        if (item == null || item.value == 0 ||item.use >= itemRow.maxTimes) {
            return r.fail("没有可以使用的次数了");
        }
        GDUdgPalace.ItemType itemType = GDUdgPalace.ItemType.transItemType(itemId);
        if (itemType == null) {
            return r.fail("不存在的道具类型");
        }
        switch (itemType) {
            case BLOOD:
                Hero hero = getHero(heroUid);
                if (hero == null) {
                    return r.fail("英雄不存在");
                }
                Map<String, Double> uidRates = hpExtend.getUidRates();
                if (uidRates == null || !uidRates.containsKey(heroUid) || uidRates.get(heroUid) >= 1d) {
                    return r.fail("该英雄无需使用生命恢复");
                }
                uidRates.put(heroUid, Math.min(1, uidRates.get(heroUid) + Integer.parseInt(itemRow.param) / 10000d));
                role.getGame().notice.message(role, "恢复成功");
                break;
            case KILL:
                r = role.getFarm().udgPalace.killBoss(bossId);
                if (!r.ok()) {
                    return r;
                }
                role.getGame().notice.message(role, "刺杀成功");
                break;
            case WISH:
                if (wishBuff.contains(heroUid)) {
                    return r.fail("目标已经被王者祝福");
                }
                hero = getHero(heroUid);
                if (hero == null) {
                    return r.fail("英雄不存在");
                }
                if (heroIsDead(heroUid)) {
                    return r.fail("英雄已阵亡无法使用");
                }
                wishBuff.add(heroUid);
                role.getGame().notice.message(role, "祝福成功");
                break;
            case DAMAGE:
                r = role.getFarm().udgPalace.killAllBoss();
                if (!r.ok()) {
                    return r;
                }
                role.getGame().notice.message(role, "毁灭成功");
                break;
            default:
                break;
        }
        item.useIt();
        return r;
    }

    public void addMallItems(int group) {
        List<UdgPalaceShopRow> list = role.getGame().table.udgPalace.shop.randomItems(group, 3);
        List<Commodity> newItems = new ArrayList<>();
        for (UdgPalaceShopRow row : list) {
            newItems.add(new Commodity(row.id));
        }
        mall.addAll(newItems);
    }

    public void recordReward(MixRes reward) {
        recordDropReward.addList(reward);
    }

    public boolean heroIsDead(String heroUid) {
        Map<String, Double> uidRates = hpExtend.getUidRates();
        return uidRates != null && uidRates.containsKey(heroUid) && uidRates.get(heroUid) <= 0;
    }

    public GameResult<Void> buyItem(String uid) {
        GameResult<Void> r = new GameResult<>();
        if (StringUtils.isBlank(uid)) {
            return r.fail("购买失败");
        }
        Commodity item = mall.stream().filter(e -> StringUtils.equals(e.uid, uid)).findAny().orElse(null);
        if (item == null) {
            return r.fail("购买失败");
        }
        UdgPalaceShopRow shopRow = role.getGame().table.udgPalace.shop.get(item.tid);
        if (shopRow == null) {
            return r.fail("商品不存在");
        }
        return new MixResConsumer<Void>()
                .tips(true)
                .res(new MixRes(shopRow.consumeStr))
                .success(role -> {
                    item.purchaseNum ++;
                    if (item.purchaseNum > 0 && item.purchaseNum >= shopRow.limit) {
                        new MixResAdder()
                                .setRes(shopRow.rewardStr)
                                .setSendMailWhenFull(true)
                                .setDialogReward(true)
                                .exe(role);
                        mall.removeIf(e -> StringUtils.equals(e.uid, uid));
                    }
                    return null;
                })
                .exe(role);
    }

    public void buffInvalid(String heroUid) {
        wishBuff.remove(heroUid);
    }
}
